<?php
class NewsRating
{
	/**
	 * 
	 * @var News
	 */
	protected $_news;
	
	protected $_positiveCount = 0;
	protected $_negativeCount = 0;
	
	public function __construct(News $news)
	{
		$this->_news = $news;
		$this->getRatings();
	}
	
	protected function getRatings()
	{
		$criteria = new Criteria();
		$criteria->add(
			NewsratingstatisticsPeer::ID_NEWS, 
			$this->_news->getIdNews(), 
			Criteria::EQUAL
		);
		
		$ratings = NewsratingstatisticsPeer::doSelect($criteria);
		
		if (empty($ratings)) {
			return;
		}
		
		//var_dump($ratings);
		foreach ($ratings as $rating) {
			if ($rating->getIsPositive()) {
				$this->_positiveCount++;
			} else {
				$this->_negativeCount++;
			}
		}
	}
	
	public function getPositive()
	{
		return $this->_positiveCount;
	}
	
	public function getNegative()
	{
		return $this->_negativeCount;
	}
	
	public function getOverall()
	{
		return $this->getPositive() - $this->getNegative();
	}
	
	public function getCounter()
	{
		return $this->getPositive() + $this->getNegative();
	}
	
	public function getPercentPositive()
	{
		if ($this->getCounter() == 0) {
			return 0;
		}
		
		return $this->getPositive() * 100 / ($this->getCounter());
	}
}